package com.xl.web.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.xl.base.annotation.Auth;
import com.xl.base.web.BaseController;
import com.xl.core.util.PasswordUtil;
import com.xl.core.util.SessionLocalUtils;
import com.xl.core.util.StringUtil;
import com.xl.web.entity.message.MessageBase;
import com.xl.web.entity.sys.SysMember;
import com.xl.web.entity.theme.MainTheme;
import com.xl.web.service.message.MessageBaseService;
import com.xl.web.service.sys.SysMemberService;

/**
 * 登录后主页跳转控制器
 * 
 * @author 梁华
 * 
 */
@Controller
@Scope("prototype")
public class MainController extends BaseController {

	// 注入service
	@Autowired(required = false)
	private SysMemberService<SysMember> memberService;
	@Autowired
	private MessageBaseService<MessageBase> messageBaseService;

	/**
	 * 主页跳转
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true)
	@RequestMapping("/main")
	public ModelAndView mian() throws Exception {

		Map<String, Object> context = getRootMap(request);
		SysMember member =SessionLocalUtils.getOperator().getMember();
		if(member.getMemMainStyle()!=null){
			MainTheme mainTheme=JSON.parseObject(member.getMemMainStyle(), MainTheme.class);
			context.put("mainTheme", mainTheme);
		}
		
		MessageBase messageBase =new MessageBase();
		messageBase.setMemberId(member.getId());
		messageBase.setMessageType("1");//未读
		int unreadCount= messageBaseService.queryCount(messageBase);
		context.put("unreadCount", unreadCount);
		
		messageBase =new MessageBase();
		messageBase.setMemberId(member.getId());
		messageBase.setStatus("4");//回复未读
		int replyUnreadCount= messageBaseService.queryCount(messageBase);
		context.put("replyUnreadCount", replyUnreadCount);
		
		return forword("main/bsa_main", context);
	}
	
	/**
	 * 平台总览
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true)
	@RequestMapping("/overview")
	public ModelAndView overview(SysMember member) throws Exception {

		Map<String, Object> context = getRootMap(request);
		return forword("sys/main_overview", context);
	}

	/**
	 * 修改密码
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true)
	@RequestMapping("/changePwd")
	public ModelAndView changePwd() throws Exception {

		Map<String, Object> context = getRootMap(request);

		return forword("main/change_pwd", context);
	}

	/**
	 * 修改密码保存
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true)
	@ResponseBody
	@RequestMapping("/changePwdSave")
	public void changePwdSave(String oldPwd, String newPwd) throws Exception {

		// 非空验证
		if (StringUtil.isEmpty(oldPwd) || StringUtil.isEmpty(newPwd)) {
			sendFailureMessage(response, "输入项有误");
			return;
		}

		// 获取到登录用户的全部数据
		SysMember sysMember = memberService.queryById(SessionLocalUtils
				.getOperator().getMember().getId());

		// 验证旧密码是否输入有误
		String password = PasswordUtil.encrypt(sysMember.getMemName(), oldPwd,
				PasswordUtil.getStaticSalt());
		if (!password.equals(sysMember.getMemPwd())) {
			sendFailureMessage(response, "旧密码错误");
			return;
		}

		// 设置新的密码
		String newPassword = PasswordUtil.encrypt(sysMember.getMemName(),
				newPwd, PasswordUtil.getStaticSalt());
		sysMember.setMemPwd(newPassword);

		memberService.updateBySelective(sysMember);

		sendSuccessMessage(response, "修改成功");
	}
}
